package cn.edu.scau.cmi.yangjiamin.springhibernate;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.List;

public class StudentDao {

	private static SessionFactory factory;

	private TeacherDao teaDao;
	private ArrayList<Student> stuList = new ArrayList<>();
	private Set<Student> students = new HashSet<Student>();// Teacher类的students属性

	static {
		try {
			factory = new Configuration().configure("cn/edu/scau/cmi/yangjiamin/springhibernate/hibernate.cfg.xml")
					.buildSessionFactory();
		} catch (Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		}

	}

	// 增
	public Integer addStu(String stu_name) {
		Session session = factory.openSession();
		Transaction tx = null;
		Integer stu_id = null;
		try {
			tx = session.beginTransaction();
			Student student = new Student(stu_name);
			stu_id = (Integer) session.save(student);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return stu_id;
	}

	// 删,通过stu_id
	public void deleteStu(Integer stu_id) {
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Student student = (Student) session.get(Student.class, stu_id);
			if(student != null) {
				session.delete(student);
			}
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	// 改
	public void updateStu(Integer stu_id, String stu_name) {
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Student student = (Student) session.get(Student.class, stu_id);
			student.setStu_name(stu_name);
			session.update(student);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	
	public ArrayList getStuList() {
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			stuList = new ArrayList<>();
			stuList = (ArrayList) session.createQuery("FROM Student").list();
			tx.commit();

		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return stuList;

	}

	//添加老师
	public void addTea(Integer stu_id, Integer tea_id) {
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Student student = (Student) session.get(Student.class, stu_id);
			Teacher teacher = (Teacher) session.get(Teacher.class, tea_id);
			if (student.getTea() != null) {
				System.out.println("该学生已经有指导教师");
			} else {
				if (teacher != null) {
					student.setTea(teacher);
					students = (Set<Student>) teacher.getStudents();
					students.add(student);// 添加学生
					teacher.setStudents(students);
					session.update(student);
				} else if (teacher == null) {
					System.out.println("没有该工号的老师");
				}
			}
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	//修改老师
	public void updateTea(Integer stu_id, Integer tea_id) {
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Student student = (Student) session.get(Student.class, stu_id);
			Teacher teacher = (Teacher) session.get(Teacher.class, tea_id);
			if (student.getTea() == null) {
				System.out.println("该学生没有指导教师，请先添加指导教师");
			} else {

				if (teacher != null) {
					student.setTea(teacher);
					students = (Set<Student>) teacher.getStudents();
					students.add(student);// 添加学生
					teacher.setStudents(students);
					session.update(student);
				} else if (teacher == null) {
					System.out.println("没有该工号的教师");
				}
			}
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public ArrayList getTeachers() {
		return teaDao.getTeaList();
	}

}
